#ifndef TCPNETWORKSERVICE_H
#define TCPNETWORKSERVICE_H

#include "inetworkservice.h"

// 具体TCP实现
class TcpNetworkService : public InetworkService
{
    Q_OBJECT
public:
    explicit TcpNetworkService(QObject *parent = nullptr);
    ~TcpNetworkService() override;

    // 接口实现
    bool connectToServer(const QString &host, quint16 port) override;
    void disconnectFromServer() override;
    bool isConnected() const override;
    QString errorString() const override;
    bool sendData(const QByteArray &data) override;
    bool sendJson(const QJsonObject &json) override;
    void setProtocolType(ProtocolType type) override;
    ProtocolType protocolType() const override;

private slots:
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError error);

private:
    QTcpSocket *socket;
    ProtocolType currentProtocol;
    QByteArray receiveBuffer;
};

#endif // TCPNETWORKSERVICE_H
